﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Collections.Specialized;
using CostTrade.BusinessLayer;
using Natrue;
using Natrue.Data;
using Natrue.Popedom.Organizational;
using Natrue.Web.MVC;
using NHibernate.Expression;

namespace CostTrade.WebApp.Controllers
{
    [HandleError]
    public class HomeController : AuthController
    {
        public const int pageSize = 30;

        [LoginAuthorizationFilter]
        public ActionResult Index()
        {
            return View();
        }

        [LoginAuthorizationFilter]
        public ActionResult Download(Guid? id)
        {
            if (id == null) return RedirectToAction("Error", "Home", new { message = "参数传递错误" });
            Attachment downloadItem =
                ProxyContainer.FindLocalProxy<IActiveRecordHelper>().GetIBODetail<Attachment>(id.Value);
            return File(Server.MapPath(downloadItem.VirtualPath), downloadItem.ContentType, downloadItem.FileName);
        }

        public ActionResult Error(string message)
        {
            ViewData["message"] = message;
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult DataDictionary()
        {
            Type[] currentRunningTypes = Assembly.GetAssembly(typeof(RegionBase)).GetTypes();
            Dictionary<string, Type> dataSource = new Dictionary<string, Type>();
            string firstType = String.Empty;
            foreach (Type runningType in currentRunningTypes)
            {
                if (Attribute.IsDefined(runningType, typeof(PropertyTypeAttribute), true))
                {
                    if (String.IsNullOrEmpty(firstType))
                        firstType = runningType.Name;
                    dataSource.Add(runningType.Name, runningType);
                }
            }
            ViewData["dictionary"] = dataSource;

            if (dataSource.Count != 0)
            {
                string currDic = Request.QueryString["dic"];
                if (String.IsNullOrEmpty(currDic))
                    currDic = firstType;
                ViewData["currDic"] = currDic;
                IActiveRecordHelper cm = ProxyContainer.FindLocalProxy<IActiveRecordHelper>();
                ViewData["dataSource"] = cm.GetIBOList(dataSource[currDic]);
            }
            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult DeskTop()
        {
            return View();
        }


        /// <summary>
        /// 设置分页信息
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public int SetPageInfo(int? pageIndex,int recordCount)
        {
            int page = pageIndex.HasValue ? pageIndex.Value : 0;
            ViewData["page"] = page;
            ViewData["pageSize"] = pageSize;
            ViewData["recordCount"] = recordCount;
            return page;
        }

        /// <summary>
        /// 获取表单中一组同名控件的值数组
        /// </summary>
        /// <param name="nameValueCollection"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public string[] GetNameValueArray(NameValueCollection nameValueCollection,string name)
        {
            return string.IsNullOrEmpty(name) ? new string[0] : nameValueCollection[name].Split(','); 
        }

        #region 数据字典

        [LoginAuthorizationFilter]
        public ActionResult DictionaryIndex(string currIboType)
        {
            IList<Type> allDicTypes = new List<Type>();
            Type[] allTypes = Assembly.GetAssembly(typeof(RegionBase)).GetTypes();
            Type dictionaryType = null;
            if (!String.IsNullOrEmpty(currIboType))
                dictionaryType = Assembly.GetAssembly(typeof(RegionBase)).GetType(currIboType);

            foreach (Type type in allTypes)
            {
                if (type.GetInterface(typeof(IDictionaryProperty).FullName) != null)
                {
                    if (dictionaryType == null) dictionaryType = type;
                    allDicTypes.Add(type);
                }
            }
            ViewData["allDicTypes"] = allDicTypes;
            ViewData["currDic"] = dictionaryType;
            return View(ArHelper.GetIBOList(dictionaryType));
        }

        #endregion

        public void GetEmployeeNameList(string q)
        {
            IList<Employee> emplist = ArHelper.GetIBOSlicedList<Employee>(0, 20, new[] { Order.Asc("EmployeeName") }, Expression.Like("EmployeeName", q, MatchMode.Anywhere).IgnoreCase());
            string str = "";
            if (emplist != null)
                foreach (Employee item in emplist)
                {
                    if (string.IsNullOrEmpty(str)) str = item.EmployeeName + "|" + item.PrimaryKey;
                    else str += "\n" + item.EmployeeName + "|" + item.PrimaryKey;
                }
            Response.Write(str);
        }

    }
}
